#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <QDebug>
#include "mypushbutton.h"
#include <QTimer>
#include <QSound>
MainScene::MainScene(QWidget *parent): QMainWindow(parent), ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //配置主要场景

    //设定固定大小
    setFixedSize(390,590);

    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    //设置标题
    setWindowTitle("翻金币");

    connect(ui->actionquit,&QAction::triggered,[=]{
        this->close();
    });

    //准备开始按钮音效
    QSound *startSound = new QSound(":/res/TapButtonSound.wav",this);


    //开始按钮
    myPushButton *startBtn = new myPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width() * 0.5 - startBtn->width()*0.5, this->height() *0.7);
    chooseScene = new ChooseLevelScene;

    //监听返回关卡的返回按钮的信号
    connect(chooseScene,&ChooseLevelScene::chooseSceneBack,this,[=]{
        //延时返回
        QTimer::singleShot(300,this,[=]{
            this->setGeometry(chooseScene->geometry());
            chooseScene->hide();
            this->show();
        });
    });

    connect(startBtn,&QPushButton::clicked,[=]{
        qDebug()<<"点击了开始按钮";

        //播放开始按钮音效
        startSound->play();

        //开始按钮动画
        startBtn->zoom();

        //延时100ms后进入下一个场景
        QTimer::singleShot(100,this,[=]{
            //设置下一场景位置
            chooseScene->setGeometry(this->geometry());

            //进入到选择场景中
            this->hide();
            chooseScene->show();
        });
    });
}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    QPixmap pix;

    //加载背景图
    pix.load(":/res/MenuSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(), pix);

    //加载小图片
    pix.load(":/res/Title.png");
    //缩放
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10, 30, pix);


}

MainScene::~MainScene()
{
    delete ui;
}

